//
//  SEPatientFileHealthViewController.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/17.
//

import UIKit

class SEPatientFileHealthViewController: SEBaseViewController {

    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.screenWidth, height: UIScreen.bottomSafeHeight))
            tableView.rowHeight = UITableView.automaticDimension
            tableView.se_header = SERefreshHeader {
                self.loadNewData()
            }
            tableView.se_footer = SERefreshFooter {
                self.loadMoreData()
            }
            tableView.se_empty = SEEmptyView { [weak self] in self?.requestPatientFileList()
            }
            tableView.register(UINib(nibName: SEPatientFileHospitalizeCell.className, bundle: nil), forCellReuseIdentifier: SEPatientFileHospitalizeCell.reuseId)
            tableView.register(UINib(nibName: SEPatientFileRegistrationCell.className, bundle: nil), forCellReuseIdentifier: SEPatientFileRegistrationCell.reuseId)
            tableView.register(UINib(nibName: SEPatientFileExamineCell.className, bundle: nil), forCellReuseIdentifier: SEPatientFileExamineCell.reuseId)
        }
    }

    var viewModel = SEPatientFileHealthViewModel()

    init(with viewModel: SEPatientDetailViewModel) {
        self.viewModel.patientViewModel = viewModel
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        requestPatientFileList()
        // Do any additional setup after loading the view.
    }

    override func setupUI() {
        super.setupUI()

        viewModel.updateData = { status in
            self.tableView.se_empty?.allowShow = true
            self.endRefreshing()
            switch status {
            case .success:
                self.tableView.reloadData()
            case .auth(let msg):
                self.presentLoginViewController(selector: #selector(self.requestPatientFileList)) {
                    SEHUD.showError(message: msg)
                }
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

}

// MARK: - UITableViewDataSource, UITableViewDelegate

extension SEPatientFileHealthViewController: UITableViewDataSource, UITableViewDelegate {

    // MARK: - UITableViewDataSource

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel.models.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let model = viewModel.models[indexPath.section]
        if model.type == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: SEPatientFileHospitalizeCell.reuseId, for: indexPath) as! SEPatientFileHospitalizeCell
            cell.updateCell(model)
            return cell
        } else if model.type == 3 {
            let cell = tableView.dequeueReusableCell(withIdentifier: SEPatientFileExamineCell.reuseId, for: indexPath) as! SEPatientFileExamineCell
            cell.update(with: model)
            return cell
        } else {
            return UITableViewCell()
        }
    }

    // MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let model = viewModel.models[indexPath.section]
        if model.type == 1 || model.type == 3 {
            return UITableView.automaticDimension
        } else {
            return 0.01
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = viewModel.models[indexPath.section]
        viewModel.selectedModel = model
        if model.type == 1 {
            /// 线上问诊详情
            pushToHospitalizeOrderDetail()
        } else if model.type == 3 {
            /// 检查订单详情
            pushToOrderExamineDetailVC()
        }
    }
}

// MARK: - Navi

extension SEPatientFileHealthViewController {
    /// 图文问诊详情
    private func pushToHospitalizeOrderDetail() {
        let vc = SEHospitalizeOrderDetailViewController(with: viewModel.selectedModel.diagData.id)
        vc.title = "问诊详情"
        navigationController?.pushViewController(vc, animated: true)
    }

    /// 跳转 订单 详情
    func pushToOrderExamineDetailVC() {
        let detailVc = SEOrderExamineDetailViewController(with: viewModel.selectedModel.checkData.applyFormId)
        detailVc.title = "检查详情"
        navigationController?.pushViewController(detailVc, animated: true)
    }
}

// MARK: - APIRequset

extension SEPatientFileHealthViewController {
    @objc private func requestPatientFileList() {
        tableView.se_header?.beginRefreshing()
    }

    private func endRefreshing() {
        tableView.se_footer_hidden = false
        tableView.endHeaderRefreshing()
        tableView.endFooterRefreshing(hasMore: viewModel.hasMore)
    }

    private func loadNewData() {
        viewModel.page = 1
        viewModel.requestPatientFileList()
    }

    private func loadMoreData() {
        viewModel.page += 1
        viewModel.requestPatientFileList()
    }
}
